*****************************************************************************************
* This do file performs all industry-level analysis.
* Outputs: Table 3, Table A3, Table C1, Figure C1, Figure C2, Figure C3, Figure C4
* Last modified by: Ludovica Gazze
* Date:07/05/2017
* Packages Needed: regife (requires reghdfe and hdfe)
*****************************************************************************************

cap log close
clear all
set more off
set scheme s1color

***Set directory
capture cd "/Users/Dropbox/Chinese food exports"


*************************
*    Table 3 Panel A    *
*************************

use "Data/Coded data/Customs/synth_hs2year_dropaffected0_collapsed", clear

local var lvalue

g dairy=hs2==100
g post=year>=2009
g dairyXpost=dairy*post
g foodXpost=food*post

qui tab year, generate(dy)
forval i=1/14 {
	forval y=1/7{
		g ctrl_`i'_`y'=value_share`y'*dy`i'
	}
}

label var dairyXpost "Dairy, Post Scandal"
label var foodXpost "Food, Post Scandal"

*** DD

reg `var' dairyXpost i.hs2 i.year if (food!=1 | hs==100), vce(cluster hs2) 
est sto `var'_main1

reg `var' dairyXpost foodXpost i.hs2 i.year , vce( cluster hs2) 
est sto `var'_main_food

reg `var' dairyXpost i.hs2 i.year ///
	i.year#(c.value_share1 c.value_share2 c.value_share3 c.value_share4 c.value_share5 c.value_share6 c.value_share7) ///
	if (food!=1 | hs==100), vce(cluster hs2)
est sto `var'_main_ctrl

reg `var' dairyXpost i.hs2 i.year i.hs2#c.year if (food!=1 | hs==100), vce( cluster hs2)
est sto `var'_main_trend

tempname texfile
file open `texfile' using "Results/main_tables/table_3_panel_a.tex", write replace
file close `texfile' 

estout lvalue_main1 lvalue_main_food lvalue_main_ctrl lvalue_main_trend using "Results/main_tables/table_3_panel_a.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(dairyXpost foodXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 

	
*************************
*    Table 3 Panel B    *
*************************	
	
use "Data/Coded data/Customs/synth_hs2year_dropaffected1_collapsed", clear

local var lvalue

g dairy=hs2==100
g post=year>=2009
g dairyXpost=dairy*post
g foodXpost=food*post

qui tab year, generate(dy)
forval i=1/14 {
	forval y=1/7{
		g ctrl_`i'_`y'=value_share`y'*dy`i'
	}
}

label var dairyXpost "Dairy, Post Scandal"
label var foodXpost "Food, Post Scandal"

*** DD

reg `var' dairyXpost i.hs2 i.year if (food!=1 | hs==100), vce(cluster hs2) 
est sto `var'_main1

reg `var' dairyXpost foodXpost i.hs2 i.year , vce( cluster hs2) 
est sto `var'_main_food

reg `var' dairyXpost i.hs2 i.year ///
	i.year#(c.value_share1 c.value_share2 c.value_share3 c.value_share4 c.value_share5 c.value_share6 c.value_share7) ///
	if (food!=1 | hs==100), vce(cluster hs2)
est sto `var'_main_ctrl

reg `var' dairyXpost i.hs2 i.year i.hs2#c.year if (food!=1 | hs==100), vce( cluster hs2)
est sto `var'_main_trend

tempname texfile
file open `texfile' using "Results/main_tables/table_3_panel_b.tex", write replace
file close `texfile' 

estout lvalue_main1 lvalue_main_food lvalue_main_ctrl lvalue_main_trend using "Results/main_tables/table_3_panel_b.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(dairyXpost foodXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 



*************************
*       Table A3        *
*************************

use "Data/ChineseCustomsData/customs_yearly_2000_2013.dta", clear
compress
rename hs hs6
gen hs2=substr(hs_id,1,2)
destring hs2, replace

keep if import==1   //keep only import data


replace hs2=100 if hs_id=="04011000" | hs_id=="04012000" | hs_id=="04013000" |    ///
hs_id=="04014000" | hs_id=="04015000" | hs_id=="04021000" | hs_id=="04022100" |    ///
hs_id=="04022900" | hs_id=="04029100" | hs_id=="04029900" | hs_id=="04031000" |    ///
hs_id=="04039000" | hs_id=="04041000" | hs_id=="04049000" | hs_id=="04051000" |    ///
hs_id=="04052000" | hs_id=="04059000" | hs_id=="04061000" | hs_id=="04062000" |     ///
hs_id=="04063000" | hs_id=="04064000" | hs_id=="04069000" | hs_id=="19011000" |    ///
hs_id=="19019000" | hs_id=="35011000" | hs_id=="35022000"


collapse (sum) value, by (hs2 year)
ren value import_value

merge 1:1 hs2 year using "Data/Coded data/Customs/synth_hs2year_dropaffected0_collapsed.dta"

keep if _m==3
drop _m

replace import_value=import_value/10^6

g limpvalue=log(import_value+0.1)

g dairy=hs2==100
g post=year>=2009
g dairyXpost=dairy*post
g foodXpost=food*post

label var dairyXpost "Dairy, Post Scandal"
label var foodXpost "Food, Post Scandal"


reg limpvalue dairyXpost i.hs2 i.year if (food!=1 | hs==100), vce(cluster hs2) 
est sto limpvalue_main1

reg limpvalue dairyXpost foodXpost i.hs2 i.year , vce( cluster hs2) 
est sto limpvalue_main_food

reg limpvalue dairyXpost i.hs2 i.year i.hs2#c.year if (food!=1 | hs==100), vce( cluster hs2)
est sto limpvalue_main_trend

tempname texfile
file open `texfile' using "Results/appendix_tables/table_a3.tex", write replace
file close `texfile' 

estout limpvalue_main1 limpvalue_main_food limpvalue_main_trend using "Results/appendix_tables/table_a3.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(dairyXpost foodXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 
	
	
	
	
**************************
*    Table C1 Panel A    *
**************************	
	
*** Interactive FE
use "Data/Coded data/Customs/synth_hs2year_dropaffected0_collapsed", clear

tsset hs2 year
regife `var' dairyXpost if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 1) vce( cluster hs2)
est sto `var'_main1

regife `var' dairyXpost if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 2) vce( cluster hs2)
est sto `var'_main2

regife `var' dairyXpost if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 3) vce( cluster hs2)
est sto `var'_main3

regife `var' dairyXpost foodXpost, absorb(hs2 year) factors(hs2 year, 1) vce( cluster hs2)
est sto `var'_main_food

regife `var' dairyXpost ctrl_1_1-ctrl_14_7 if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 1) vce( cluster hs2)
est sto `var'_main_ctrl

tempname texfile
file open `texfile' using "Results/appendix_tables/table_c1_panel_a.tex", write replace
file close `texfile' 

estout lvalue_main1 lvalue_main2 lvalue_main3 lvalue_main_food lvalue_main_ctrl  using "Results/appendix_tables/table_c1_panel_a.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(dairyXpost foodXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 


	
	
**************************
*    Table C1 Panel B    *
**************************	

*** Interactive FE
use "Data/Coded data/Customs/synth_hs2year_dropaffected1_collapsed", clear

tsset hs2 year
regife `var' dairyXpost if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 1) vce( cluster hs2)
est sto `var'_main1

regife `var' dairyXpost if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 2) vce( cluster hs2)
est sto `var'_main2

regife `var' dairyXpost if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 3) vce( cluster hs2)
est sto `var'_main3

regife `var' dairyXpost foodXpost, absorb(hs2 year) factors(hs2 year, 1) vce( cluster hs2)
est sto `var'_main_food

regife `var' dairyXpost ctrl_1_1-ctrl_14_7 if (food!=1 | hs==100), absorb(hs2 year) factors(hs2 year, 1) vce( cluster hs2)
est sto `var'_main_ctrl

tempname texfile
file open `texfile' using "Results/appendix_tables/table_c1_panel_b.tex", write replace
file close `texfile' 

estout lvalue_main1 lvalue_main2 lvalue_main3 lvalue_main_food lvalue_main_ctrl  using "Results/appendix_tables/table_c1_panel_b.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(dairyXpost foodXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 



************************
*       Figure C1      *
************************

local y=2008
local trper=`y'+1
local f=0

use "Data/Coded data/Customs/synth_hs2year_dropaffected0_collapsed", clear

drop if food==1 & hs!=100
local preyear=`y'

***synth control
tsset hs year

forvalue i=1(1)6{

  replace value_share`i'=0 if value_share`i'==.
		
	}

synth lvalue lvalue entry_hsyear value_share1 value_share2 value_share3 ///
	value_share4 value_share5 value_share6, ///
	trunit(100) xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
	trperiod(`trper') nested allopt fig replace
		
matrix list e(V_matrix)
matrix define V_weights=e(V_matrix)
 
***save graphs
graph export "Results/appendix_figures/figure_c1.eps", as(eps) replace
		
clear 
svmat V_weights
sa "Results/appendix_tables/table_c2_panel_b_c1", replace


************************
*       Figure C3      *
************************


local y=2008
local trper=`y'+1
local f=0

use "Data/Coded data/Customs/synth_hs2year_dropaffected1_collapsed", clear

drop if food==1 & hs!=100
local preyear=`y'

***synth control
tsset hs year

forvalue i=1(1)6{

  replace value_share`i'=0 if value_share`i'==.
		
	}

synth lvalue lvalue entry_hsyear value_share1 value_share2 value_share3 ///
	value_share4 value_share5 value_share6, ///
	trunit(100) xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
	trperiod(`trper') nested allopt fig replace
		
matrix list e(V_matrix)
matrix define V_weights=e(V_matrix)
 
***save graphs
graph export "Results/appendix_figures/figure_c3.eps", as(eps) replace
		
clear 
svmat V_weights
sa "Results/appendix_tables/table_c2_panel_b_c2", replace




************************
*       Figure C2      *
************************


local y=2008
local trper=`y'+1

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	levelsof hs, local(industry)
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}

	foreach  j in 1 6 13 14 23 25 26 27 28 29 {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}
}
 

local min 31
local max 39

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}


	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}
	
}






local min 40
local max 49

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}

	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}
}




local min 50
local max 59

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}


	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}

}



local min 60
local max 69

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}


	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}	
	
}



local min 70
local max 79

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}


	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}

}


local min 80
local max 89

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}


	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}
	
}



local min 90
local max 99

forv d=0/1 {

	local preyear=`y'
	use "Data/Coded data/Customs/synth_hs2year_dropaffected`d'_collapsed", clear
	
	preserve
		keep if hs>=`min' & hs<=`max'
		levelsof hs, local(industry)

	restore 
	drop if food==1 & hs!=100
	tsset hs year
	drop if hs==100 //the treated unit should not be in the control pool
	forvalue i=1(1)6{
		replace value_share`i'=0 if value_share`i'==.
	}


	foreach  j of local industry {
		synth lvalue entry_hsyear lvalue value_share1 value_share2 value_share3 ///
			value_share4 value_share5 value_share6, ///
			trunit(`j') xperiod(2000(1)`preyear') mspeperiod(2000(1)`preyear') ///
			trperiod(`trper') nested allopt  keep("Results/synth/synth`j'_d`d'")
	}

}
	
	
use "Data/Coded data/Customs/synth_hs2year_dropaffected0_collapsed", clear

drop if (food==1 & hs!=100) | hs==30

levelsof hs, local(industry)

foreach j of local industry {

	append_empty using "Results/synth/synth`j'_d`d'", generate(new`j')

   }

 
g hs=.


foreach j of local industry {

	replace hs=`j' if new`j'==1
	drop new`j'
	
	}
	
	
gen dif=_Y_treated-_Y_synthetic
g large_dif1=abs(dif)>15 & _time==2008
bys hs: egen large_dif=max(large_dif1)
g dif1=dif if large_dif==1
replace dif=. if large_dif==1	

gen dairy_dif=.
replace dairy_dif=dif if hs==100
replace dif=. if hs==100


foreach j of local industry {

	local gr `gr' line dif _time if hs == `j', color(gs14) ||

	}
	
label var _time "year"
graph twoway `gr' (line dairy_dif _time, color(black)), xlabel(2000(1)2013) ///
   xlabel(,angle(45)) ytitle("Difference between Treated and Control Units", size(medlarge)) ///
   xline(2009, lcolor(red)) legend(off)

graph export "Results/figures/placebo_dairy_dropaffected0.eps", as(eps) replace

	
************************
*       Figure C4      *
************************

use "Data/Coded data/Customs/synth_hs2year_dropaffected1_collapsed", clear

drop if (food==1 & hs!=100) | hs==30

levelsof hs, local(industry)

foreach j of local industry {

	append_empty using "Results/synth/synth`j'_d`d'", generate(new`j')

   }

 
g hs=.


foreach j of local industry {

	replace hs=`j' if new`j'==1
	drop new`j'
	
	}
	
	
gen dif=_Y_treated-_Y_synthetic
g large_dif1=abs(dif)>15 & _time==2008
bys hs: egen large_dif=max(large_dif1)
g dif1=dif if large_dif==1
replace dif=. if large_dif==1	

gen dairy_dif=.
replace dairy_dif=dif if hs==100
replace dif=. if hs==100


foreach j of local industry {

	local gr `gr' line dif _time if hs == `j', color(gs14) ||

	}
	
label var _time "year"
graph twoway `gr' (line dairy_dif _time, color(black)), xlabel(2000(1)2013) ///
   xlabel(,angle(45)) ytitle("Difference between Treated and Control Units", size(medlarge)) ///
   xline(2009, lcolor(red)) legend(off)

graph export "Results/figures/placebo_dairy_dropaffected1.eps", as(eps) replace
